package apptoko.com.adapter;

import java.util.List;
import java.util.Locale;

import main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu.OnItemSelectedListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.RatingBar;
import android.widget.TextView;
import apptoko.com.UILApplication;
import apptoko.com.model.ContentItemInfo;
import apptoko.com.util.Utils;

import com.androidquery.AQuery;
import apptoko.com.R;
;

@SuppressLint("NewApi")
public class ContentItemAdapter extends ArrayAdapter<ContentItemInfo> {

	private LayoutInflater li;
	private String store;
	private AQuery aq;
	private Activity context;
	private int greenColor;
	private int purpleColor;
	private Resources res;
	private OnPopupMenuClickListener listener;
	public ContentItemAdapter(Activity context, int textViewResourceId,
			List<ContentItemInfo> list) {
		super(context, textViewResourceId, list);
		// TODO Auto-generated constructor stub
		this.context = context;
		li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		greenColor = context.getResources().getColor(R.color.app_indicator);
		purpleColor = context.getResources().getColor(R.color.gift_indicator);
		res = context.getResources();
	}

	public void setOnPopupMenuClickListener(OnPopupMenuClickListener l) {
		this.listener = l;
	}

	public void setStore(String store) {
		this.store = store;
	}

	private int getColor(String store) {
		int colorId = R.color.app_indicator;
		if (store.equalsIgnoreCase("apps") || store.equalsIgnoreCase("games")) {
			colorId = R.color.app_indicator;
		} else if (store.equalsIgnoreCase("books")) {
			colorId = R.color.ebook_indicator;
		} else if (store.equalsIgnoreCase("comics")) {
			colorId = R.color.comic_indicator;
		} else if (store.equalsIgnoreCase("movies")) {
			colorId = R.color.film_indicator;
		}
		return context.getResources().getColor(colorId);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = li.inflate(R.layout.short_content_item, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.appName = (TextView) convertView.findViewById(R.id.name);
			holder.author = (TextView) convertView.findViewById(R.id.author);
			holder.rating = (RatingBar) convertView.findViewById(R.id.rating);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.priceUnit = (ImageView) convertView.findViewById(R.id.price_unit);
			holder.btnQuickMenu = (ImageButton) convertView.findViewById(R.id.btn_quick_menu);
			holder.ribbonGift = (ImageView) convertView.findViewById(R.id.ribbon_gift);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ContentItemInfo item = getItem(position);
		if (item != null) {
			aq = new AQuery(convertView);
			if(item.getGiftbox() == 1){
				aq.id(holder.ribbonGift).visible();
			} else {
				aq.id(holder.ribbonGift).gone();
			}
			if(UILApplication.getInstance().isLoadAllImgs()){
				aq.id(holder.icon).visibility(View.VISIBLE);
				Bitmap placeholder = aq.getCachedImage(R.drawable.image_loading);
					if(aq.getCachedFile(item.getAvatar())!=null){
						aq.id(holder.icon).image(aq.getCachedFile(item.getAvatar()), 0);
						Log.e("NMN","load collection items from cachedFile");
					}else{
						Log.e("NMN","load collection items from server");
						aq.id(holder.icon).image(item.getAvatar(), false, true, 0, 0,placeholder, AQuery.FADE_IN_NETWORK);
					}
			}else{
				aq.id(holder.icon).visibility(View.GONE);
			}
			
			aq.id(holder.appName).text((position + 1) + ". " + item.getTitle());
			aq.id(holder.author).text(item.getAuthor());
			float rate;
			if (item.getTotalRate() > 0 && item.getSumRate()>0) {
//				rate = item.getSumRate() / item.getTotalRate();
				rate = item.getTotalRate()/item.getSumRate() ;
			} else {
				rate = 0;
			}
			aq.id(holder.rating).rating(rate);
			if (store.equalsIgnoreCase("apps") || store.equalsIgnoreCase("home") || store.equalsIgnoreCase("games")) {
				if (item.isInstalled()) {
					aq.id(holder.price).text(res.getString(R.string.installed).toUpperCase(Locale.US));
					aq.id(holder.priceUnit).invisible();
					aq.id(holder.btnQuickMenu).visible().clicked(new OnClickListener() {
								@Override
								public void onClick(View v) {
									if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
										PopupMenu menu = new PopupMenu(context, v);
										menu.getMenuInflater().inflate(R.menu.installed_app_menu, menu.getMenu());
										menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
											@Override
											public boolean onMenuItemClick(MenuItem menuItem) {
												// TODO Auto-generated method stub
												switch (menuItem.getItemId()) {
												case R.id.menu_launch:
													Intent launch = context.getPackageManager().getLaunchIntentForPackage(item.getPackageName());
													launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
													context.startActivity(launch);
													break;
												case R.id.menu_uninstall:
													Utils.uninstallAPK(context, item.getPackageName());
													break;
												}
												return true;
											}
										});
										menu.show();
									} else {
										main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
										popupMenuCompat.add(1, R.string.menu_launch);
										popupMenuCompat.add(2, R.string.menu_uninstall);
										popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
											@Override
											public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
												switch (menuItem.getItemId()) {
												case 1:
													Intent launch = context.getPackageManager().getLaunchIntentForPackage(item.getPackageName());
													launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
													context.startActivity(launch);
													break;
												case 2:
													Utils.uninstallAPK(context, item.getPackageName());
													break;
												}
											}
										});
										popupMenuCompat.show(v);
									}
								}
							});
				} else {
					if (item.getPrice() == 0 && item.getPrice2() == 0) {
						aq.id(holder.price).text(res.getString(R.string.free).toUpperCase(Locale.US));
						aq.id(holder.priceUnit).invisible();
					}
//					else if (item.getPrice() == 0 && item.getPrice2() != 0) {
//						aq.id(holder.price).text(String.valueOf(item.getPrice2())).textColor(purpleColor);
//						aq.id(holder.priceUnit).image(R.drawable.tym_purple).visible();
//					} else if (item.getPrice() != 0 && item.getPrice2() == 0) {
//						aq.id(holder.price).text(String.valueOf(item.getPrice())).textColor(greenColor);
//						aq.id(holder.priceUnit).image(R.drawable.tym_green).visible();
//					}
					aq.id(holder.btnQuickMenu).visible().clicked(new OnClickListener() {
								@Override
								public void onClick(View v) {
									if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
										PopupMenu menu = new PopupMenu(context, v);
										menu.getMenuInflater().inflate(R.menu.not_installed_app_menu,menu.getMenu());
										menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
											@Override
											public boolean onMenuItemClick(MenuItem menuItem) {
												// TODO Auto-generated method stub
												switch (menuItem.getItemId()) {
												case R.id.menu_install:
													if (listener != null) {
														listener.onInstallClicked(store, item);
													}
													break;
												case R.id.menu_add_wishlist:
													if (listener != null) {
														listener.onAddToWishListClicked(store, item);
													}
													break;
												}
												return true;
											}
										});
										menu.show();
									} else {
										main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
										popupMenuCompat.add(1, R.string.install);
										popupMenuCompat.add(2, R.string.add_to_wishlist);
										popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
											@Override
											public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
												switch (menuItem.getItemId()) {
												case 1:
													if (listener != null) {
														listener.onInstallClicked(store, item);
													}
													break;
												case 2:
													if (listener != null) {
														listener.onAddToWishListClicked(store, item);
													}
													break;
												}
											}
										});
										popupMenuCompat.show(v);
									}
								}
							});
				}
			} else if (store.equalsIgnoreCase("books") || store.equalsIgnoreCase("comics")) {
				if (item.getPrice() == 0 && item.getPrice2() == 0) {
					aq.id(holder.price)
							.text(res.getString(R.string.free).toUpperCase(
									Locale.US))
							.textColor(getColor(store));
					aq.id(holder.priceUnit).invisible();
				} else if (item.getPrice() == 0 && item.getPrice2() != 0) {
					aq.id(holder.price).text(String.valueOf(item.getPrice2()))
							.textColor(purpleColor);
					aq.id(holder.priceUnit).image(R.drawable.tym_purple).visible();
				} else if (item.getPrice() != 0 && item.getPrice2() == 0) {
					aq.id(holder.price).text(String.valueOf(item.getPrice()))
							.textColor(greenColor);
					aq.id(holder.priceUnit).image(R.drawable.tym_green).visible();
				}
				aq.id(holder.btnQuickMenu).visible()
						.clicked(new OnClickListener() {
							@Override
							public void onClick(View v) {
								if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
									PopupMenu menu = new PopupMenu(context, v);
									menu.getMenuInflater().inflate(R.menu.book_comic_menu, menu.getMenu());
									menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
										@Override
										public boolean onMenuItemClick(
												MenuItem menuItem) {
											// TODO Auto-generated method stub
											switch (menuItem.getItemId()) {
											case R.id.menu_download:
												if (listener != null) {
													listener.onDownloadClicked(store, item);
												}
												break;
											case R.id.menu_add_wishlist:
												if (listener != null) {
													listener.onAddToWishListClicked(store, item);
												}
												break;
											}
											return true;
										}
									});
									menu.show();
								} else {
									main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
									popupMenuCompat.add(1, R.string.download);
									popupMenuCompat.add(2, R.string.add_to_wishlist);
									popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
										@Override
										public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
											switch (menuItem.getItemId()) {
											case 1:
												if (listener != null) {
													listener.onDownloadClicked(store, item);
												}
												break;
											case 2:
												if (listener != null) {
													listener.onAddToWishListClicked(store, item);
												}
												break;
											}
										}
									});
									popupMenuCompat.show(v);
								}
							}
						});
			} else if (store.equalsIgnoreCase("movies")) {
				if (item.getPrice() == 0 && item.getPrice2() == 0) {
					aq.id(holder.price).text(res.getString(R.string.free).toUpperCase(Locale.US)).textColor(getColor(store));
					aq.id(holder.priceUnit).invisible();
				} else if (item.getPrice() == 0 && item.getPrice2() != 0) {
					aq.id(holder.price).text(String.valueOf(item.getPrice2())).textColor(purpleColor);
					aq.id(holder.priceUnit).image(R.drawable.tym_purple).visible();
				} else if (item.getPrice() != 0 && item.getPrice2() == 0) {
					aq.id(holder.price).text(String.valueOf(item.getPrice())).textColor(greenColor);
					aq.id(holder.priceUnit).image(R.drawable.tym_green).visible();
				}
				aq.id(holder.btnQuickMenu).visible().clicked(new OnClickListener() {
							@Override
							public void onClick(View v) {
								if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
									PopupMenu menu = new PopupMenu(context, v);
									menu.getMenuInflater().inflate(R.menu.movie_menu, menu.getMenu());
									menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
										@Override
										public boolean onMenuItemClick(MenuItem menuItem) {
											// TODO Auto-generated method stub
											switch (menuItem.getItemId()) {
//											case R.id.menu_watch:
//												if (listener != null) {
//													listener.onWatchClicked(item);
//												}
//												break;
											case R.id.menu_add_wishlist:
												if (listener != null) {
													listener.onAddToWishListClicked(store, item);
												}
												break;
											}
											return true;
										}
									});
									menu.show();
								} else {
									main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu popupMenuCompat = new main.java.com.u1aryz.android.lib.newpopupmenu.PopupMenu(context);
									//popupMenuCompat.add(1, R.string.watch);
									popupMenuCompat.add(2, R.string.add_to_wishlist);
									popupMenuCompat.setOnItemSelectedListener(new OnItemSelectedListener() {
										@Override
										public void onItemSelected(main.java.com.u1aryz.android.lib.newpopupmenu.MenuItem menuItem) {
											switch (menuItem.getItemId()) {
//											case 1:
//												if (listener != null) {
//													listener.onWatchClicked(item);
//												}
//												break;
											case 2:
												if (listener != null) {
													listener.onAddToWishListClicked(store, item);
												}
												break;
											}
										}
									});
									popupMenuCompat.show(v);
								}
							}
						});
			}
		}
		return convertView;
	}

	public static class ViewHolder {
		ImageView icon;
		TextView appName;
		TextView author;
		RatingBar rating;
		TextView price;
		ImageView priceUnit;
		ImageButton btnQuickMenu;
		ImageView ribbonGift;
	}

}
